package com.example.project4;

import java.io.IOException;
import java.io.Serializable;
import java.nio.ByteBuffer;

class Header implements Serializable{
	int method;
	int length;
	int indexes;
	
	public Header(int method, int lenth, int indexes){
		this.method = method;
    	this.length = lenth;
    	this.indexes = indexes;
	}	
	
	public Header(byte [] bytes){
	       
	    int i;
	    ByteBuffer wrapped;
		
	    byte [] method = new byte[4];
	    for (i=0; i<4; i++){
	    	method[i] = bytes[i];
	    }
	    wrapped = ByteBuffer.wrap(method);
	    this.method = wrapped.order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
	    
	    byte [] length= new byte[4];
	    for (i=0; i<4; i++){
	    	length[i] = bytes[i+4];
	    }
	    wrapped = ByteBuffer.wrap(length);
	    this.length = wrapped.order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
	    
	    byte [] indexes = new byte[4];
	    for (i=0; i<4; i++){
	    	indexes[i] = bytes[i+8];
	    }
	    wrapped = ByteBuffer.wrap(indexes);
	    this.indexes = wrapped.order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
	    
	}
	
	
	public int getLength(){
		return this.length;
	}
	
	public int getIndexes(){
		return this.indexes;
	}   
	
	private void writeObject(java.io.ObjectOutputStream stream) throws IOException {
            stream.writeInt(method);
            stream.writeInt(length);
            stream.writeInt(indexes);
    }	

	public byte[] getHeaderByteArray(){	
		int [] hearderIA = {this.method, this.length, this.indexes};
		byte [] headerBA = new byte[12];
		for(int i = 0; i < hearderIA.length; i++) {
			headerBA[i*4] = (byte)(hearderIA[i] & 0xFF);
			headerBA[i*4 + 1] = (byte)((hearderIA[i] & 0xFF00) >>> 8);
			headerBA[i*4 + 2] = (byte)((hearderIA[i] & 0xFF0000) >>> 16);
			headerBA[i*4 + 3] = (byte)((hearderIA[i] & 0xFF000000) >>> 24);
	    }
		return headerBA;		
	}
	
	public String toString(){
		return Integer.toString(this.method) +","+ Integer.toString(this.length) +","+ Integer.toString(this.indexes);
	}
	
	/**
	public void setHeaderValues(int method, int lenth, int indexes){
		this.method = method;
    	this.length = lenth;
    	this.indexes = indexes;
	} 
    */
}
